package by.grsu.epam.domain.account;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import by.grsu.epam.domain.service.Service;
import by.grsu.epam.domain.user.impl.Subscriber;

@Entity
public class Account {
	private long id;
	private Integer balance;
	private List<Service> services;
	private List<Bill> history;
	private TelephoneNumber telephoneNumber;
	private boolean accountIsNotLocked;
	private Subscriber subscriber;
	
	public Account() {
		balance = 0;
		services = new ArrayList<Service>();
		history = new ArrayList<Bill>();
		setAccountIsNotLocked(true);
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Integer getBalance() {
		return balance;
	}

	public void setBalance(Integer balance) {
		this.balance = balance;
	}

	@ManyToMany
	@JoinTable(name = "AccountService", 
			joinColumns = {@JoinColumn(name = "accountId")},
			inverseJoinColumns = {@JoinColumn(name = "serviceId")})
	public List<Service> getServices() {
		return services;
	}

	public void setServices(List<Service> services) {
		this.services = services;
	}

	@OneToMany(targetEntity = Bill.class, mappedBy = "account",
			cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	public List<Bill> getHistory() {
		return history;
	}

	public void setHistory(List<Bill> history) {
		this.history = history;
	}

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name = "telephonenumber_id")
	public TelephoneNumber getTelephoneNumber() {
		return telephoneNumber;
	}

	public void setTelephoneNumber(TelephoneNumber telephoneNumber) {
		this.telephoneNumber = telephoneNumber;
	}

	public boolean isAccountIsNotLocked() {
		return accountIsNotLocked;
	}

	public void setAccountIsNotLocked(boolean accountIsNotLocked) {
		this.accountIsNotLocked = accountIsNotLocked;
	}

	@ManyToOne
	@JoinColumn(name = "subscriberId")
	public Subscriber getSubscriber() {
		return subscriber;
	}

	public void setSubscriber(Subscriber subscriber) {
		this.subscriber = subscriber;
	}
}
